package com.briup.demo.controller;
import java.util.Date;
import java.util.List;

import com.briup.demo.bean.XcdOrder;
import com.briup.demo.service.XcdOrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.briup.demo.common.Response;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "订单处理")
@RestController
@RequestMapping("/order")

public class XcdOrderController {
    @Autowired
    private XcdOrderService orderService;

    @ApiOperation(value = "获取所有订单",notes = "从数据库检索所有订单信息返回")
    @GetMapping("/all")
    public Response<List<XcdOrder>> queryAll(){
    	
        List<XcdOrder> cs = orderService.getAllMember();

        return Response.ok(cs);
    }

    @ApiOperation(value = "按照条件检索商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin",value = "开始时间"),
            @ApiImplicitParam(name = "end",value = "结束时间"),
            @ApiImplicitParam(name = "key",value = "商品信息")
    })
    @GetMapping("/searchBy")
    public Response<List<XcdOrder>> searchBy(
            Date begin, Date end, String key){
        System.out.println("begin:"+begin);
        System.out.println("end:"+end);
        List<XcdOrder> as =
                orderService.searchBy(begin, end, key);
        return Response.ok(as);
    }

    @ApiOperation(value = "按照订单编号，删除订单信息！")
    @DeleteMapping("/deleteByOrderId")
    public Response<String> deleteById(String id){
        //调用订单方法
        int num = orderService.deleteByOrderId(id);
        //根据处理结果，返回不同的数据。
        if(num==-1) {
            return Response.error("请先删除所有子订单!");
        }
        if(num==0) {
            return Response.error("此订单不存在！");
        }
        return Response.ok("成功删除！");
    }


    @ApiOperation(value = "保存或者更新订单信息" ,
            notes = "如果给定订单编号，那么代表修改订单信息<br/>"
                    + "如果没有给定订单编号，那么代表新增订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "OrderId",
                    value = "订单编号,更新时必须传递"),
            @ApiImplicitParam(name = "GoodsId",value = "货物编号"),
            @ApiImplicitParam(name = "BuyerId",value = "购买者ID"),
            @ApiImplicitParam(name = "BuyerName",value = "购买者名字"),
    })
    @PostMapping("/saveOrUpdate")
    public Response<String> saveOrUpdateArticle(
            String OrderId,String GoodsId,String BuyerId,String BuyerName){
        int b =
                orderService.saveOrUpdate(OrderId, GoodsId, BuyerId, BuyerName);
        if(b==-1) return Response.ok("不存在此订单信息");
        else if(b==1) return Response.ok("新订单保存成功");
        else return Response.ok("订单更新成功");
    }


}
